package cn.wh.xwm.controller.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.hibernate.Transaction;

import cn.wh.xwm.controller.action.vo.ProductView;
import cn.wh.xwm.hibernate.dao.Productinfo;
import cn.wh.xwm.hibernate.dao.ProductinfoDAO;

/**
 * 产品管理 action
 * @author swimming
 *
 */
public class ProductAction extends BaseAction {
	private String name;
	private String status;
	private String id;
	
	private String page;
	private String rows;
	
	
	/**
	 * 查询产品
	 * @throws IOException
	 */
	public void findAllProduct() throws IOException{
		ProductinfoDAO dao = new ProductinfoDAO();
		List<Productinfo> list = dao.findBypage(page,rows,"Productinfo");
		List<ProductView> vlist = new ArrayList<ProductView>();
		for(Productinfo info :list){
			ProductView view = new ProductView();
			view.setId(info.getId());
			view.setName(info.getName());
			view.setStatus(info.getValid().equals(0)?"无效":"有效");
			vlist.add(view);
		}
		JSONObject object = new JSONObject();
		object.put("rows", vlist);
		object.put("total", dao.findTotalCount("Productinfo"));
		dao.getSession().close();
		this.responseJson(object);
 	}
	
	/**
	 * 增加产品
	 * @return
	 * @throws IOException 
	 */
	public void addProduct() throws IOException{
		ProductinfoDAO dao = new ProductinfoDAO();
		Productinfo info = new Productinfo();
		info.setName(this.getName());
		info.setValid(this.getStatus().equals("1")?1:0);
		Transaction ts = dao.getSession().getTransaction();
		ts.begin();
		dao.save(info);
		ts.commit();
		JSONObject object = new JSONObject();
		object.put("success", true);
		dao.getSession().close();
		this.responseJson(object);
	}
	
	/**
	 * 修改产品
	 * @throws IOException 
	 */
	public void updateProduct() throws IOException{
		ProductinfoDAO dao = new ProductinfoDAO();
		Productinfo info = dao.findById(this.getId());
		info.setName(this.getName());
		info.setValid(this.getStatus().equals("1")?1:0);
		Transaction ts = dao.getSession().getTransaction();
		ts.begin();
		dao.update(info);
		ts.commit();
		JSONObject object = new JSONObject();
		object.put("success", true);
		dao.getSession().close();
		this.responseJson(object);
	}
	
	/**
	 *删除产品
	 * @throws IOException 
	 */
	public void deleteProduct() throws IOException{
		ProductinfoDAO dao = new ProductinfoDAO();
		Productinfo info = dao.findById(this.getId());
		Transaction ts = dao.getSession().getTransaction();
		ts.begin();
		dao.delete(info);
		ts.commit();
		JSONObject object = new JSONObject();
		object.put("success", true);
		dao.getSession().close();
		this.responseJson(object);
	}

	
	
	
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}
	
	
}
